package logic

import (
	"common/comCopier"
	"common/dao/model"
	"context"
	"gorm.io/gorm"

	"gateway/internal/svc"
	"gateway/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserInfoLogic {
	return &UserInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserInfoLogic) UserInfo() (*types.UserInfo, error) {
	// todo: add your logic here and delete this line
	tokenInfo := l.ctx.Value("token")
	user := model.User{}
	err := comCopier.CopyWithOption(&user, tokenInfo)
	if err != nil {
		return nil, err
	}
	resp := types.UserInfo{}
	db := l.svcCtx.DB
	table := db.Table(model.TableNameUser)
	err = table.Transaction(func(tx *gorm.DB) error {

		if err = tx.Where("uuid = ?", user.UUID).Find(&user).Error; err != nil {
			return err
		}
		_ = comCopier.CopyWithOption(&resp, user)
		resp.Password = ""
		return nil
	})
	if err != nil {
		return nil, err
	}
	return &resp, nil
}
